package leetcode;

/**
 * @author kkyeer
 * @description: 1162. 地图分析
 * 你现在手里有一份大小为 N x N 的『地图』（网格） grid，上面的每个『区域』（单元格）都用 0 和 1 标记好了。其中 0 代表海洋，1 代表陆地，你知道距离陆地区域最远的海洋区域是是哪一个吗？请返回该海洋区域到离它最近的陆地区域的距离。
 *
 * 我们这里说的距离是『曼哈顿距离』（ Manhattan Distance）：(x0, y0) 和 (x1, y1) 这两个区域之间的距离是 |x0 - x1| + |y0 - y1| 。
 *
 * 如果我们的地图上只有陆地或者海洋，请返回 -1。
 *
 *
 *
 * 示例 1：
 *
 * 输入：[[1,0,1],[0,0,0],[1,0,1]]
 * 输出：2
 * 解释：
 * 海洋区域 (1, 1) 和所有陆地区域之间的距离都达到最大，最大距离为 2。
 *
 * 示例 2：
 *
 * 输入：[[1,0,0],[0,0,0],[0,0,0]]
 * 输出：4
 * 解释：
 * 海洋区域 (2, 2) 和所有陆地区域之间的距离都达到最大，最大距离为 4。
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/as-far-from-land-as-possible
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @date:Created in 20:12 3-29
 * @modified By:
 */
public class MaxDistance_1162 {
    public static void main(String[] args) {
//        int[][] grid = new int[][]{{1,1,1,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,1,0,0,0,1,1,0,1,0,1,1,1,0},{1,0,1,0,0,1,1,1,0,0,1,0,1,1,0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,0,1,0,1,0,0,0,1,1,0},{1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,1,0,0,1,1,0,1,1,1},{0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,1,0,0,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,0},{0,0,0,0,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0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1,1,0,0,0,0,1,0,0,0},{1,1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,0,1,0,0,0,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,0,1},{0,0,1,0,0,0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,0,0,1,1,1,0,1,0,0,1,1,0,0,1,1,0,0,1,1,1},{1,0,0,1,0,0,1,1,0,0,1,0,1,0,0,1,1,0,0,0,0,1,0,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,0},{1,0,0,0,1,1,0,1,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1,0,0,1,0,1,0,1},{1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1},{0,1,0,0,0,1,1,1,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,1,0,0,1,1,0,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,0,1,0,1,0,0,0,1,1,1,1,1},{0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,1,1,1,1,0,0,1,0,0,1,1,0,1},{1,0,1,0,1,0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,1,0,1,0,0,0,0,1},{1,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,1,1,1,0,0}};
//        int[][] grid = new int[][]{{0,0,1,1,1},{0,1,1,0,0},{0,0,1,1,0},{1,0,0,0,0},{1,1,0,0,1}};
//        int[][] grid = new int[][]{{1, 0, 1}, {0, 0, 0}, {1, 0, 1}};
        int[][] grid = new int[][]{{1, 0, 0}, {0, 0, 0}, {0, 0, 0}};
        System.out.println(new MaxDistance_1162().maxDistance(grid));
    }

    public int maxDistance(int[][] grid){
        int[][] dist = new int[grid.length][];
        for (int i = 0; i < grid.length; i++) {
            dist[i] = new int[grid.length];
        }
        int up;
        int left;
        int sum = 0;
        int max = -1;
        // 左上到右下
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid.length; j++) {
                if (grid[i][j] == 0) {
                    if (i == 0) {
                        left = j > 0 ? dist[i][j - 1] : 300;
                        dist[i][j] = left + 1;
                    }else if (j == 0) {
                        up = dist[i - 1][j];
                        dist[i][j] = up + 1;
                    }else {
                        dist[i][j] = Math.min(dist[i-1][j], dist[i][j - 1]) + 1;
                    }
                }else {
                    sum ++;
                }
            }
        }
        if (sum == 0 || sum == grid.length * grid.length) {
            return -1;
        }
        // 右下到左上
        int right;
        int down;
        for (int i = grid.length - 1; i >= 0; i--) {
            for (int j = grid.length - 1; j >= 0; j--) {
                if (grid[i][j] == 0) {
                    if (i == grid.length - 1) {

                    } else if (j == grid.length - 1) {
                        down = dist[i + 1][j];
                        dist[i][j] = Math.min(down + 1, dist[i][j]);
                    } else {
                        down = dist[i + 1][j];
                        right = dist[i][j + 1];
                        dist[i][j] = Math.min(dist[i][j], Math.min(down, right) + 1);
                    }
                }
            }
        }
        // 左下到右上
        for (int i = grid.length - 1; i >= 0; i--) {
            for (int j =0; j < grid.length; j++) {
                if (grid[i][j] == 0) {
                    if (i == grid.length - 1) {

                    } else if (j == 0) {
                        down = dist[i + 1][j];
                        dist[i][j] = Math.min(down + 1, dist[i][j]);
                    } else {
                        down = dist[i + 1][j];
                        left = dist[i][j -1];
                        dist[i][j] = Math.min(dist[i][j], Math.min(down, left) + 1);
                    }
                }
            }
        }
        // 右上到左下
        for (int i = 0; i < grid.length; i++) {
            for (int j = grid.length - 1; j >= 0; j--) {
                if (grid[i][j] == 0) {
                    if (i == 0) {

                    } else if (j == grid.length - 1) {
                        up = dist[i - 1][j];
                        dist[i][j] = Math.min(up + 1, dist[i][j]);
                    } else {
                        up = dist[i - 1][j];
                        right = dist[i][j + 1];
                        dist[i][j] = Math.min(dist[i][j], Math.min(up, right) + 1);
                    }
                    max = Math.max(max, dist[i][j]);
                }
            }
        }
        return max;
    }
}
